﻿#region License
// (c) Garry McGlennon 2009
// 
// This project is made available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// 
// View the project and the latest code at http://dnpframework.codeplex.com/
// 
// All other rights reserved.
// 
// blog: www.dotNetProfessional.com/blog/
#endregion
using System;

namespace dnp.Framework.Web
{
    public class IPAddress
    {
        /// <summary>
        /// Given a text version of an IP Address convert it to its numeric version.
        /// </summary>
        /// <param name="ipAddress">IP Address in dot notation</param>
        /// <returns>Numeric IP Address</returns>
        public static long IPAddressToNumber(string ipAddress)
        {
            string[] parts = ipAddress.Split('.');
            if (parts.Length != 4)
                throw new ArgumentException("Invalid IP Address: " + ipAddress);

            // C# doesn't support a simple power syntax!
            return (long.Parse(parts[0]) * 256 * 256 * 256) + (int.Parse(parts[1]) * 256 * 256) + (int.Parse(parts[2]) * 256) + int.Parse(parts[3]);
        }
    }
}
